package com.andyliu.sys.service.impl;

import com.andyliu.sys.mapper.SUserMapper;
import com.andyliu.sys.pojo.SUser;
import com.andyliu.sys.pojo.UserDto;
import com.andyliu.sys.service.UserService;
import com.andyliu.vo.ParamVO;
import com.andyliu.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName: UserServiceImpl
 * Description: 用户业务实现
 * date: 2021/1/20 2:49 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private SUserMapper mapper;
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean deleteUsers(Integer[] ids) {
        Boolean status = false;
        int flag =0;
        for(Integer id:ids){
            flag+=mapper.deleteByPrimaryKey(id);
        }

        return flag==ids.length?true:false;
    }


    @Override
    public Boolean saveUser(SUser user) {
        Boolean status = false;
        int flag =0;
        if(user.getId()!=null){
            flag=mapper.updateByPrimaryKeySelective(user);
        }else{
            flag = mapper.insertSelective(user);
        }
        status = flag==1?true:false;
        return status;
    }

    @Override
    public Boolean deleteUserById(Integer id) {
        int flag = mapper.deleteByPrimaryKey(id);
        return flag==1?true:false;
    }

    @Override
    public SUser queryUserById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<UserVO> queryUserByPage(ParamVO<SUser> user, Integer pageNumber, Integer pageSize) {
       SUser u = user.getParam();
        PageHelper.startPage(pageNumber,pageSize);
        List<UserVO> users = mapper.selectUsers(u);

        return new PageInfo<UserVO>(users);
    }

    @Override
    public UserVO selectUser(String userName, String pwd) {
        SUser user = new SUser();
        user.setUsername(userName);
        user.setPwd(pwd);
        List<UserVO> users = mapper.selectUserByUserNameAndPwd(user);
        if(users!=null && users.size()==1){
            return users.get(0);
        }
        return null;
    }
}
